﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace TeamWork_Client
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        public UserDetails User = null;

        private void commandLink1_Click(object sender, EventArgs e)
        {
            string credentials = textBox1.Text + "|" + textBox2.Text;
            Dictionary<string, string> headers = new Dictionary<string, string>();
            headers.Add("TYPE", "Login");
            headers.Add("uCred", credentials);
            HttpWebResponse res = HTTPManager.SendMessage2(Form1.server + ":55987", "", headers);

            string msg = res.Headers["LG_RESULT"];
            if(msg == "OK")
            {
                BinaryReader reader = new BinaryReader(res.GetResponseStream());
                UserDetails udetails = new UserDetails();
                udetails.Username = reader.ReadString();
                udetails.Password = reader.ReadString();
                udetails.Role = reader.ReadString();
                udetails.ID = reader.ReadInt32();
                reader.Close();

                User = udetails;
                MessageBox.Show("User has been logged in!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }else
            {
                MessageBox.Show(msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}
